home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zfdcte.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  4.5 KB  |  146 lines

  1. /* Copyright (C) 1994, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zfdcte.c,v 1.3 2000/09/19 19:00:53 lpd Exp $ */
  20. /* DCTEncode filter creation */
  21. #include "memory_.h"
  22. #include "stdio_.h"        /* for jpeglib.h */
  23. #include "jpeglib_.h"
  24. #include "ghost.h"
  25. #include "oper.h"
  26. #include "gsmalloc.h"        /* for gs_memory_default */
  27. #include "ialloc.h"
  28. #include "idict.h"
  29. #include "idparam.h"
  30. #include "strimpl.h"
  31. #include "sdct.h"
  32. #include "sjpeg.h"
  33. #include "ifilter.h"
  34. #include "iparam.h"
  35.  
  36. /*#define TEST*/
  37.  
  38. /* Import the parameter processing procedure from sdeparam.c */
  39. stream_state_proc_put_params(s_DCTE_put_params, stream_DCT_state);
  40. #ifdef TEST
  41. stream_state_proc_get_params(s_DCTE_get_params, stream_DCT_state);
  42. #endif
  43.  
  44. /* <target> <dict> DCTEncode/filter <file> */
  45. private int
  46. zDCTE(i_ctx_t *i_ctx_p)
  47. {
  48.     os_ptr op = osp;
  49.     gs_memory_t *mem = &gs_memory_default;
  50.     stream_DCT_state state;
  51.     dict_param_list list;
  52.     jpeg_compress_data *jcdp;
  53.     int code;
  54.     const ref *dop;
  55.     uint dspace;
  56.  
  57.     /* First allocate space for IJG parameters. */
  58.     jcdp = (jpeg_compress_data *)
  59.     gs_alloc_bytes_immovable(mem, sizeof(*jcdp), "zDCTE");
  60.     if (jcdp == 0)
  61.     return_error(e_VMerror);
  62.     if (s_DCTE_template.set_defaults)
  63.     (*s_DCTE_template.set_defaults) ((stream_state *) & state);
  64.     state.data.compress = jcdp;
  65.     jcdp->memory = state.jpeg_memory = mem;    /* set now for allocation */
  66.     state.report_error = filter_report_error;    /* in case create fails */
  67.     if ((code = gs_jpeg_create_compress(&state)) < 0)
  68.     goto fail;        /* correct to do jpeg_destroy here */
  69.     /* Read parameters from dictionary */
  70.     if (r_has_type(op, t_dictionary))
  71.     dop = op, dspace = r_space(op);
  72.     else
  73.     dop = 0, dspace = 0;
  74.     if ((code = dict_param_list_read(&list, dop, NULL, false, iimemory)) < 0)
  75.     goto fail;
  76.     if ((code = s_DCTE_put_params((gs_param_list *) & list, &state)) < 0)
  77.     goto rel;
  78.     /* Create the filter. */
  79.     jcdp->template = s_DCTE_template;
  80.     /* Make sure we get at least a full scan line of input. */
  81.     state.scan_line_size = jcdp->cinfo.input_components *
  82.     jcdp->cinfo.image_width;
  83.     jcdp->template.min_in_size =
  84.     max(s_DCTE_template.min_in_size, state.scan_line_size);
  85.     /* Make sure we can write the user markers in a single go. */
  86.     jcdp->template.min_out_size =
  87.     max(s_DCTE_template.min_out_size, state.Markers.size);
  88.     code = filter_write(i_ctx_p, 0, &jcdp->template,
  89.             (stream_state *) & state, dspace);
  90.     if (code >= 0)        /* Success! */
  91.     return code;
  92.     /* We assume that if filter_write fails, the stream has not been
  93.      * registered for closing, so s_DCTE_release will never be called.
  94.      * Therefore we free the allocated memory before failing.
  95.      */
  96. rel:
  97.     iparam_list_release(&list);
  98. fail:
  99.     gs_jpeg_destroy(&state);
  100.     gs_free_object(mem, jcdp, "zDCTE fail");
  101.     return code;
  102. }
  103.  
  104. #ifdef TEST
  105. #include "stream.h"
  106. #include "files.h"
  107. /* <dict> <filter> <bool> .dcteparams <dict> */
  108. private int
  109. zdcteparams(i_ctx_t *i_ctx_p)
  110. {
  111.     os_ptr op = osp;
  112.     stream *s;
  113.     dict_param_list list;
  114.     int code;
  115.  
  116.     check_type(*op, t_boolean);
  117.     check_write_file(s, op - 1);
  118.     check_type(op[-2], t_dictionary);
  119.     /* The DCT filters copy the template.... */
  120.     if (s->state->template->process != s_DCTE_template.process)
  121.     return_error(e_rangecheck);
  122.     code = dict_param_list_write(&list, op - 2, NULL, iimemory);
  123.     if (code < 0)
  124.     return code;
  125.     code = s_DCTE_get_params((gs_param_list *) & list,
  126.                  (stream_DCT_state *) s->state,
  127.                  op->value.boolval);
  128.     iparam_list_release(&list);
  129.     if (code >= 0)
  130.     pop(2);
  131.     return code;
  132. }
  133. #endif
  134.  
  135. /* ------ Initialization procedure ------ */
  136.  
  137. const op_def zfdcte_op_defs[] =
  138. {
  139. #ifdef TEST
  140.     {"3.dcteparams", zdcteparams},
  141. #endif
  142.     op_def_begin_filter(),
  143.     {"2DCTEncode", zDCTE},
  144.     op_def_end(0)
  145. };
  146.